package com.example.thridhero.webchatfragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by thridHero on 2017/3/24.
 */

public class FriendsAdapter extends RecyclerView.Adapter<FriendsAdapter.ViewHolder>{
    private List<Friends> mfrends;

    public FriendsAdapter(List<Friends> friendses){
        mfrends = friendses;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.friends_item,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                Friends friends = mfrends.get(position);
                FriendChat friendChat = new FriendChat();
                friendChat.setContentList(friends.getContent());
                Intent intent = new Intent();
                FriendChat.onActionStart(parent.getContext(),friends);
            }
        });
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        return mfrends.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Friends friends = mfrends.get(position);
        holder.friendName.setText(friends.getUserName());
        holder.frendContent.setText(friends.getEndMessage());
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.format(friends.getSendTime());
        holder.friendDate.setText("昨天");
        holder.userHeadImage.setImageResource(friends.getImageResource());
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView userHeadImage;
        TextView friendName;
        TextView friendDate;
        TextView frendContent;
        public ViewHolder(View itemView) {
            super(itemView);
            userHeadImage = (ImageView) itemView.findViewById(R.id.friend_image);
            friendName     = (TextView) itemView.findViewById(R.id.friend_name);
            friendDate     = (TextView) itemView.findViewById(R.id.friend_date);
            frendContent   = (TextView) itemView.findViewById(R.id.friend_content);
        }
    }
}
